package com.sgj.interceptor;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @author 沈国纪
 * @date 2021/3/30 14:32
 */
public class LoginInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		// 若 session 中没有 user，则重定向到登陆页面
		if (Objects.isNull(request.getSession().getAttribute("user"))) {
			response.sendRedirect("/admin");
			return false;
		}
		return true;
	}
}
